﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class test_frameset : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //以下调试临时数据

            //Session["sUserName"] = "shout";
            //Session["sUserName1"] = "周晓东";
            //Session["sRole"] = "学员";
            //Session["sJigou"] = "远程教育平台";
            //Session["sTittle"] = "学生考试";
            
            //Session["skaoshiid"] = "64";
            //Session["skaoshiname"] = "西北之旅";


            //以上调试用临时数据



            Session["skaoshiid"] = Request.QueryString["rid"];
            Conn zconn = new Conn();
            //已通过参数取得课程id
            //以下取得课程name
            string zsql = "select name from kecheng where id=" + Session["skaoshiid"].ToString();
            Session["skaoshiname"] = zconn.sqlFirst(zsql);

            //以下从套题库中取得所有题套并随机选择一个,保存于Session["skaoshitaoti"]
            string zsql0 = "select count(id) from tb_TaoTi where LessonID=" + Session["skaoshiid"].ToString().Trim() ; //取得该课程的套数
            string zsql1 = "select id,shijian from tb_TaoTi where LessonID=" + Session["skaoshiid"].ToString().Trim() ; //取得课程对应的所有套题
            int Tcount = Int16.Parse(zconn.sqlFirst(zsql0));
            string[] zTaoti = new string[Tcount];
            string[] zshijian = new string[Tcount];
            SqlDataReader zreader1 = zconn.Reader(zsql1);
            int i = 0;
            
            while (zreader1.Read())
            {
                zTaoti[i] = zreader1.GetValue(0).ToString();
                zshijian[i] = zreader1.GetValue(1).ToString();
                    
                i = i + 1;
            }
            Random ro = new Random();
            //int iUP = Tcount - 1;
            int iUP = Tcount;
            int iDown = 0;
            int ztaotinum = ro.Next(iDown, iUP);    //产生一个大于或者等于iDown,小于iUP的随机数

            //ztaotinum = 0;    //调试用

            
            Session["skaoshitaoti"] = zTaoti[ztaotinum];   //随机选择一套题
            Session["skaoshishijian"] = zshijian[ztaotinum];
            Session["sScoreDanxuan"]="0";   //本套题目各类型试题的总分
            Session["sScoreDuoxuan"]="0";
            Session["sScoreWenda"] = "0";   //预先赋值，以免发生找不到session变量的情况。
            zreader1.Close();


            //------------调试--------------//
            //Session["skaoshitaoti"] = "19";

            //------------调试--------------//
            
            //结束套题操作
            //计算选择的套题的各类型题目分数
            //zconn = new Conn();
            //string zsql = "select sum(score) from tb_questions where (que_lessonid=" + Session["skaoshiid"].ToString() + "and que_taotiid=" + Session["skaoshitaoti"].ToString() + ") group by que_type";
            //SqlDataReader reader1 = zconn.Reader(zsql);
            //上述表述有BUG，建立在每种类型都有的假设之上，按照单选、多选、问答的顺序。但假如只有问答题，则该种表述得出的结果是单选题而不是问答题。
            zsql = "select sum(score) from tb_questions where (que_lessonid=" + Session["skaoshiid"].ToString() + "and que_taotiid=" + Session["skaoshitaoti"].ToString() + " and que_type='单选题')";
            Session["sScoreDanxuan"] = zconn.sqlFirst(zsql);    //当没找到相关记录时，返回'0'，恰好是需要的结果。
            zsql = "select sum(score) from tb_questions where (que_lessonid=" + Session["skaoshiid"].ToString() + "and que_taotiid=" + Session["skaoshitaoti"].ToString() + " and que_type='多选题')";
            Session["sScoreDuoxuan"] =zconn.sqlFirst(zsql);
            zsql = "select sum(score) from tb_questions where (que_lessonid=" + Session["skaoshiid"].ToString() + "and que_taotiid=" + Session["skaoshitaoti"].ToString() + " and que_type='问答题')";
            Session["sScoreWenda"] = zconn.sqlFirst(zsql);
            zconn.Dispose();
            //计算完毕


        }
    }
}
